#!/bin/bash
# 验证 moparse 静态编译效果的测试脚本

set -e

echo "========================================="
echo "moparse 静态编译验证脚本"
echo "========================================="
echo ""

# 颜色定义
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color

print_pass() {
    echo -e "${GREEN}✓${NC} $1"
}

print_warn() {
    echo -e "${YELLOW}⚠${NC} $1"
}

print_fail() {
    echo -e "${RED}✗${NC} $1"
}

# 检查二进制文件
check_binary() {
    local binary=$1
    local name=$2
    
    echo "----------------------------------------"
    echo "检查: $name"
    echo "----------------------------------------"
    
    if [ ! -f "$binary" ]; then
        print_fail "文件不存在: $binary"
        return 1
    fi
    
    print_pass "文件存在: $binary"
    
    # 文件大小
    local size=$(ls -lh "$binary" | awk '{print $5}')
    echo "  文件大小: $size"
    
    # 文件类型
    if command -v file &> /dev/null; then
        local filetype=$(file "$binary")
        echo "  文件类型: $filetype"
        
        if echo "$filetype" | grep -q "statically linked"; then
            print_pass "完全静态链接"
        elif echo "$filetype" | grep -q "dynamically linked"; then
            print_warn "动态链接"
        fi
    fi
    
    # 动态库依赖检查
    if command -v ldd &> /dev/null; then
        echo ""
        echo "  动态库依赖检查:"
        local ldd_output=$(ldd "$binary" 2>&1)
        
        if echo "$ldd_output" | grep -q "not a dynamic executable"; then
            print_pass "完全静态链接，无动态库依赖"
        else
            echo "$ldd_output" | while read line; do
                echo "    $line"
            done
            
            # 统计依赖数量
            local dep_count=$(echo "$ldd_output" | grep "=>" | wc -l)
            if [ $dep_count -le 3 ]; then
                print_pass "动态库依赖较少 ($dep_count 个)"
            else
                print_warn "动态库依赖较多 ($dep_count 个)"
            fi
        fi
    fi
    
    # 符号表检查
    if command -v nm &> /dev/null; then
        local symbols=$(nm -D "$binary" 2>&1 | wc -l)
        if [ $symbols -lt 10 ]; then
            print_pass "符号表精简 ($symbols 个符号)"
        fi
    fi
    
    # 运行版本检查
    echo ""
    echo "  运行测试:"
    if "$binary" --version &> /dev/null || "$binary" -h &> /dev/null; then
        print_pass "程序可正常执行"
        "$binary" --version 2>&1 || "$binary" -h 2>&1 | head -n 1
    else
        print_warn "无法获取版本信息"
    fi
    
    echo ""
}

# 检查 C++ 版本
if [ -f "moparse" ]; then
    check_binary "moparse" "C++ 版本 (部分静态)"
fi

# 检查 Rust 版本
RUST_RELEASE="moparse-rs/target/release/moparse"
RUST_MUSL="moparse-rs/target/x86_64-unknown-linux-musl/release/moparse"

if [ -f "$RUST_RELEASE" ]; then
    check_binary "$RUST_RELEASE" "Rust 版本 (GNU release)"
fi

if [ -f "$RUST_MUSL" ]; then
    check_binary "$RUST_MUSL" "Rust 版本 (musl 静态)"
fi

echo "========================================="
echo "验证完成"
echo "========================================="
echo ""
echo "编译建议："
echo "  • 开发环境: make 或 ./build.sh release"
echo "  • 生产部署: make static 或 ./build.sh musl"
echo "  • 跨平台: ./build.sh musl (Rust版本)"
echo ""
echo "详细文档: STATIC_COMPILATION.md"
